package com.stgl.action;

import com.stgl.fileimage.ImgEditor;
import com.stgl.service.LoginService;
import com.stgl.util.MD5;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.stgl.util.ExportExcel;

@Controller
@EnableAutoConfiguration
@RequestMapping("/")
public class login {
    @Resource
    private LoginService loginService;


    @RequestMapping("onlogin.html")
    @ResponseBody
    public Map<String,Object>onlogin(HttpServletRequest request, HttpServletResponse response){
        String uname=request.getParameter("uname").trim();
        String upwd=request.getParameter("upwd").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("uname",uname);
        gmap.put("upwd", MD5.MD5(upwd));
        Map<String,Object>rmap=loginService.onlogin(gmap);
        if(rmap.get("result").toString().equals("true")){
            HttpSession session=((HttpServletRequest)request).getSession();
            if(rmap.get("USID")!=null){
                session.setAttribute("usid",rmap.get("USID").toString());
            }
            if(rmap.get("UNAME")!=null){
                session.setAttribute("uname",rmap.get("UNAME").toString());
            }
            if(rmap.get("ROLE")!=null){
                session.setAttribute("role",rmap.get("ROLE").toString());
            }
            if(rmap.get("USERNAME")!=null){
                session.setAttribute("username",rmap.get("USERNAME").toString());
            }
            if(rmap.get("WXID")!=null){
                session.setAttribute("wxid",rmap.get("WXID").toString());
            }
            if(rmap.get("CSID")!=null){
                session.setAttribute("csid",rmap.get("CSID").toString());

                Map<String,Object>smap=new HashMap<String,Object>();
                smap.put("csid",rmap.get("CSID"));
                Map<String,Object>getmap=loginService.getsystemInfo(smap);
                if(getmap.get("SYSTEMNAME")!=null){
                    rmap.put("SYSTEMNAME",getmap.get("SYSTEMNAME"));
                }
                if(getmap.get("WHKSH")!=null){
                    rmap.put("WHKSH",getmap.get("WHKSH").toString());
                }
                if(getmap.get("STKSH")!=null){
                    rmap.put("STKSH",getmap.get("STKSH").toString());
                }
                if(getmap.get("OTHERSH")!=null){
                    rmap.put("OTHERSH",getmap.get("OTHERSH").toString());
                }
                if(getmap.get("CSID")!=null){
                    rmap.put("CSID",getmap.get("CSID"));
                }

            }
            if(rmap.get("CSNAME")!=null){
                session.setAttribute("csname",rmap.get("CSNAME").toString());
            }
            rmap.put("jsessionid",session.getId());
        }
        return rmap;
    }

    @RequestMapping("userInfo.html")
    @ResponseBody
    public Map<String,Object>userInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>gmap=new HashMap<String,Object>();
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("usid")!=null){
            gmap.put("USID",session.getAttribute("usid").toString());
            gmap.put("login",true);
        }else{
            gmap.put("login",false);
        }
        if(session.getAttribute("uname")!=null){
            gmap.put("UNAME",session.getAttribute("uname").toString());
        }
        if(session.getAttribute("role")!=null){
            gmap.put("ROLE",session.getAttribute("role").toString());
        }
        if(session.getAttribute("username")!=null){
            gmap.put("USERNAME",session.getAttribute("username").toString());
        }

        if(session.getAttribute("wxid")!=null){
            gmap.put("WXID",session.getAttribute("wxid").toString());
        }
        if(session.getAttribute("csid")!=null){
            gmap.put("CSID",session.getAttribute("csid").toString());

            Map<String,Object>smap=new HashMap<String,Object>();
            smap.put("csid",session.getAttribute("csid").toString());
            Map<String,Object>getmap=loginService.getsystemInfo(smap);
            if(getmap.get("SYSTEMNAME")!=null){
                gmap.put("SYSTEMNAME",getmap.get("SYSTEMNAME"));
            }
            if(getmap.get("WHKSH")!=null){
                gmap.put("WHKSH",getmap.get("WHKSH"));
            }
            if(getmap.get("STKSH")!=null){
                gmap.put("STKSH",getmap.get("STKSH"));
            }
            if(getmap.get("OTHERSH")!=null){
                gmap.put("OTHERSH",getmap.get("OTHERSH").toString());
            }
            if(getmap.get("CSID")!=null){
                gmap.put("CSID",getmap.get("CSID"));
            }
        }
        if(session.getAttribute("csname")!=null){
            gmap.put("CSNAME",session.getAttribute("csname").toString());
        }
        gmap.put("jsessionid",session.getId());
        //获取系统基本信息

        return gmap;
    }



    @RequestMapping("/filesUpload.html")
    public @ResponseBody
    List<Object> filesUpload(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request, HttpSession session) throws IOException {
        List<Object> fileurllist=new ArrayList<Object>();

        Map<String, Object> fileMap = new HashMap<String, Object>();
        if(files!=null&&files.length>0){
            //循环获取file数组中得文件
            for(int i = 0;i<files.length;i++){
                MultipartFile file = files[i];
                //保存文件
                Map<String, Object> fileurl = new ImgEditor().uploadFilemap(file,request,session);
                fileurllist.add(fileurl);
            }
        }
        return fileurllist;
    }

    @RequestMapping("/downloadfile.html")
    public void downloadfile(HttpServletRequest request, HttpServletResponse resp,HttpSession session) throws IOException {
        String fileurl=request.getParameter("fileurl").trim();
        ExportExcel.downloadfile(resp, request,fileurl);
    }


    @RequestMapping("getHomeData.html")
    @ResponseBody
    public Map<String,Object>getHomeData(HttpServletRequest request, HttpServletResponse response){
        HttpSession session=((HttpServletRequest)request).getSession();
        String csid="";
        if(session.getAttribute("csid")!=null){
           csid=session.getAttribute("csid").toString();
        }
        //文化课报名统计
        List<Map<String,Object>>whk=loginService.getWhkStatic(csid);
        //社团辅导班报名统计
        List<Map<String,Object>>stfdb=loginService.getSthdStatic(csid);
        //最新报名统计
        List<Map<String,Object>>newstatic=loginService.getMostNewBm(csid);
        Map<String, Object> home = new HashMap<String, Object>();
        home.put("whk",whk);
        home.put("stfdb",stfdb);
        home.put("newstatic",newstatic);
        return home;
    }


    @RequestMapping("updateUaccount.html")
    @ResponseBody
    public Map<String,Object> updateUaccount(HttpServletRequest request, HttpServletResponse response){
        String olduname=request.getParameter("olduname").trim();
        String uname=request.getParameter("uname").trim();
        String upwd=request.getParameter("upwd").trim();
        String oupwd=request.getParameter("oupwd").trim();
        Map<String,Object>rmap=new HashMap<>();
        String usid="";
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("usid")!=null){
            usid=session.getAttribute("usid").toString();
        }else{
            return rmap;
        }
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("usid",usid);
        gmap.put("olduname",olduname);
        gmap.put("uname",uname);
        gmap.put("newupwd",MD5.MD5(upwd));
        gmap.put("oupwd",MD5.MD5(oupwd));
        Map<String,Object>getmap=loginService.updateUaccount(gmap);
        if(getmap.get("result").toString().equals("true")){
            session.removeAttribute("usid");
            session.removeAttribute("uname");
            session.removeAttribute("role");
            session.removeAttribute("username");
            session.removeAttribute("wxid");
            session.removeAttribute("csid");
            session.removeAttribute("csname");
        }
        return getmap;
    }


    @RequestMapping("getsystemInfo.html")
    @ResponseBody
    public Map<String,Object>getsystemInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>gmap=new HashMap<String,Object>();
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
            Map<String,Object>getmap=loginService.getsystemInfo(gmap);
            return getmap;
        }
        return gmap;
    }

    @RequestMapping("getsystemSet.html")
    @ResponseBody
    public Map<String,Object>getsystemSet(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>gmap=new HashMap<String,Object>();
        String csid="";
        if(request.getParameter("csid")!=null){
            csid=request.getParameter("csid").trim();
        }else{
            HttpSession session=((HttpServletRequest)request).getSession();
            if(session.getAttribute("csid")!=null){
                csid= session.getAttribute("csid").toString();
            }
        }
        gmap.put("csid",csid);
        Map<String,Object>getmap=loginService.getsystemInfo(gmap);
        return getmap;
    }


    @RequestMapping("updateSystemInfo.html")
    @ResponseBody
    public boolean updateSystemInfo(HttpServletRequest request, HttpServletResponse response){
        String whksh=request.getParameter("whksh").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("whksh",whksh);
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
            int i=loginService.updateSystemInfo(gmap);
            if(i>0){
                return true;
            }
        }
        return false;
    }


    @RequestMapping("updateSystemInfoStk.html")
    @ResponseBody
    public boolean updateSystemInfoStk(HttpServletRequest request, HttpServletResponse response){
        String stksh=request.getParameter("stksh").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("stksh",stksh);
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
            int i=loginService.updateSystemInfo(gmap);
            if(i>0){
                return true;
            }
        }
        return false;
    }



    @RequestMapping("updateSystemName.html")
    @ResponseBody
    public boolean updateSystemName(HttpServletRequest request, HttpServletResponse response){
        String csid="";
        if(request.getParameter("csid")!=null){
            csid=request.getParameter("csid").trim();
        }

        Map<String,Object>gmap=new HashMap<String,Object>();
        if(request.getParameter("systemname")!=null){
            String systemname=request.getParameter("systemname").trim();
            gmap.put("systemname",systemname);
        }

        if(request.getParameter("whkfdb")!=null){
            String whkfdb=request.getParameter("whkfdb").trim();
            gmap.put("whkfdb",whkfdb);
        }

        if(request.getParameter("sthdfdb")!=null){
            String sthdfdb=request.getParameter("sthdfdb").trim();
            gmap.put("sthdfdb",sthdfdb);
        }

        if(request.getParameter("whklsfdb")!=null){
            String whklsfdb=request.getParameter("whklsfdb").trim();
            gmap.put("whklsfdb",whklsfdb);
        }

        if(request.getParameter("sthdlsfdb")!=null){
            String sthdlsfdb=request.getParameter("sthdlsfdb").trim();
            gmap.put("sthdlsfdb",sthdlsfdb);
        }

        if(request.getParameter("whkbmqk")!=null){
            String whkbmqk=request.getParameter("whkbmqk").trim();
            gmap.put("whkbmqk",whkbmqk);
        }

        if(request.getParameter("sthdbmqk")!=null){
            String sthdbmqk=request.getParameter("sthdbmqk").trim();
            gmap.put("sthdbmqk",sthdbmqk);
        }

        if(request.getParameter("othersh")!=null){
            String othersh=request.getParameter("othersh").trim();
            gmap.put("othersh",othersh);
        }

        if(request.getParameter("topimage1s")!=null){
            String topimage1s=request.getParameter("topimage1s").trim();
            gmap.put("topimage1s",topimage1s);
        }
        if(request.getParameter("topimage2s")!=null){
            String topimage2s=request.getParameter("topimage2s").trim();
            gmap.put("topimage2s",topimage2s);
        }
        if(request.getParameter("topimage3s")!=null){
            String topimage3s=request.getParameter("topimage3s").trim();
            gmap.put("topimage3s",topimage3s);
        }
        if(request.getParameter("topimage1")!=null){
            String topimage1=request.getParameter("topimage1").trim();
            gmap.put("topimage1",topimage1);
        }
        if(request.getParameter("topimage2")!=null){
            String topimage2=request.getParameter("topimage2").trim();
            gmap.put("topimage2",topimage2);
        }
        if(request.getParameter("topimage3")!=null){
            String topimage3=request.getParameter("topimage3").trim();
            gmap.put("topimage3",topimage3);
        }

        if(csid!=null && csid!=""){
            gmap.put("csid",csid);
        }else{
            HttpSession session=((HttpServletRequest)request).getSession();
            if(session.getAttribute("csid")!=null) {
                gmap.put("csid", session.getAttribute("csid").toString());
            }
        }
        if(gmap.get("csid")!=null){
            int i=loginService.updateSystemInfo(gmap);
            if(i>0){
                return true;
            }
        }
        return false;
    }


}
